﻿#region

using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Vin.Extension.Framework.Converters.Json;
using Vin.Tool.Core.JsonCategory;

#endregion

namespace Vin.Extension.Framework;

public static class VinFrameworkExtension
{
    private static bool _isInit = false;
    public static bool IsInit => _isInit;

    private static VinFrameworkOption _vinFrameworkOption = null!;
    public static Type VinAuthorizeModelType => _vinFrameworkOption.VinAuthorizeModelType;

    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="vinFrameworkOption"></param>
    public static void InitVinFrameworkExtension(VinFrameworkOption? vinFrameworkOption = null)
    {
        if (IsInit) return;
        JsonTool.AddCustomConverter(new JTokenConverter());
        // 增加其他编码器
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        _isInit = true;
        _vinFrameworkOption = vinFrameworkOption ?? new VinFrameworkOption();
        // 设置Json序列化默认配置
        JsonConvert.DefaultSettings = () =>
        {
            var setting = new JsonSerializerSettings();
            setting.NullValueHandling = NullValueHandling.Ignore;
            // 增加此句话会影响SqlSugar的内部序列化，导致无法正常使用
            // setting.ContractResolver = new CamelCasePropertyNamesContractResolver();
            return setting;
        };
    }
}